<?php
namespace app\controller;

use app\BaseController;
use app\model\member;
use think\facade\Cache;
use think\facade\Request;
use think\facade\Session;

class Login extends BaseController
{
    public function index()
    {
        //请求信息
        $phone = Request::post( 'phone' );
        $password = Request::post( 'password' );

        //执行时间
        Cache::clear();
        event( 'user_login' );

        //查询用户
        if( $user = member::where( 'phone', $phone )->find() )
        {
            $password = md5( md5( $password ) . $user->seed );  //密码加密方式

            if( $password == $user->password )
            {
                //生成token
                $token = jiami( $user->member_id, $user->seed );

                //注册全局登录状态
                Session::set( 'member_id', $user->member_id );

                //csrf token
                $csrf_token = $this->request->buildToken( '_csrf_token', 'sha1' );

                return $this->response( [ 'status' => 1, 'message' => '授权成功', 'auth_token' => $token, 'csrf_token' => $csrf_token ] );
            }else{
                return $this->response( [ 'status' => -1, 'message' => '密码错误' ] );
            }
        }

        return $this->response( [ 'status' => 0, 'message' => '未查询到用户信息' ] );
    }
}
